package com.net.oil.oilWear.model;

import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.custom.BaseObserverNoT;
import com.net.oil.oilWear.contract.AddOilWearContract;
import com.net.oil.oilWear.presenter.AddOilWearPresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：lch
 * 时间：2019/1/20 0020:下午 22:11
 * 邮箱：245850413@qq.com
 * 说明：
 */
public class AddOilWearModel extends BaseModel implements AddOilWearContract.Model {

    private AddOilWearPresenter presenter;

    public AddOilWearModel(AddOilWearPresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void addFillOilRecord(int carId, int currentMileage, double quantity, final double price, double oilMoney, String date) {
        AppHttpClient.ApiService().addFillOilRecord(carId, currentMileage, quantity, price, oilMoney, date)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean!= null){
                            presenter.addFillOilRecordSuccess();
                        }else {
                            presenter.addFillOilRecordFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.addFillOilRecordFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void updateFillOilRecord(int id, int currentMileage, double quantity, double price, double oilMoney) {
        AppHttpClient.ApiService().updateFillOilRecord(id, currentMileage, quantity, price, oilMoney)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.updateFillOilRecordSuccess();
                        }else {
                            presenter.updateFillOilRecordFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.updateFillOilRecordFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
